﻿using ConfExpert.Forms.DockingControls;
using ConfExpert.Forms.TaskManager;
using ConfExpert.Utility;
using ConfExpert.Utility.Ribbon;

using DevTeamIT;
using Extensions;
using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms
{
    public partial class MainForm : DevMainForm
    {
        public MainForm()
        {
            InitializeComponent();

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //if (Session.AmI(4, 17, 94, 95, 120))

            if (AppSession.Settings.SkinName != string.Empty)
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(AppSession.Settings.SkinName);
            else
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("Office 2013");

            OkuYetki();

            if (Session.DataManager.ExecuteScalar("CheckOP", WhoAmI).ToString() != string.Empty)
                JobManager.ShowPopup("Onayınızı bekleyen OP girişleri var!", "Stok girişi yapılan toplar için, Depo->İmalatFazlası->OnayBekleyen formundan tercih bildirmeniz gerekiyor.");

            Session.SetAdmins(4, 17, 94, 95, 284, 285, 192);
            Session.SetDevelopers(17, 94, 95, 284, 285);

            Session.OncekiKonf = new clsOncekiKonf();
            Session.OncekiKonf.KonfTarihi = DateTime.Today;

            Session.OncekiYukleme = new clsOncekiYukleme();
            Session.OncekiYukleme.YuklemeTarihi = DateTime.Today;

            ConfExpertRibbon.Init();
            Mail.Init();
            AppSession.EasyNav = new Araclar.EasyNav();

            FileManager.CreateTempFolder("CEDocs", true);

            JobManager.Job jobPublish = JobManager.Add("Yeni bir sürüm var!", 600, false);
            jobPublish.Alert += jobPublish_Alert;

            JobManager.Job jobOnay = JobManager.Add("Onayınızı bekleyen istekler var!", 600, true);
            jobOnay.Alert += jobOnay_Alert;

            JobManager.Job jobTasks = JobManager.Add("Görevler", 1200, true);
            jobTasks.Alert += jobTasks_Alert;

            JobManager.Job jobYeniSiparis = JobManager.Add("Onayınızı bekleyen yeni siparişler var!", 540, true);
            jobYeniSiparis.Alert += jobYeniSiparis_Alert;
            jobOnay.PopupClicked += jobYeniSiparis_PopupClicked;

            JobManager.CheckAlerts();

            Reports reports = new Reports(nbReports, txtReportFilter, btnReportClearFilter);
            CustomerBasedOperations cbo = new CustomerBasedOperations(nbCustomer, gleCustomer);
            SeasonBasedOperations sbo = new SeasonBasedOperations(nbSeason, gleSeason);
            ribbon.SelectedPage = ribbon.Pages[2];

            this.Show();
        }

        public static void OkuYetki()
        {
            DataSet dsYetki = Session.DataManager.ExecuteDataSet("GetYetki");
            DataTable dt = dsYetki.Tables[0];
            DataTable dtU = dsYetki.Tables[1];
            DataTable dtD = dsYetki.Tables[2];

            Session.ytkDict = new Dictionary<int, clsYetki>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int YetkiId = (int)dt.Rows[i]["YetkiId"];
                List<int> U = new List<int>();
                foreach (DataRow r in dtU.Select("YetkiId=" + YetkiId.ToStr()))
                    U.Add((int)r["UserId"]);

                List<int> D = new List<int>();
                foreach (DataRow r in dtD.Select("YetkiId=" + YetkiId.ToStr()))
                    D.Add((int)r["DepartmentId"]);

                Session.ytkDict.Add(YetkiId, new clsYetki(U, D));
            }
        }

        void jobTasks_Alert(JobManager.Job popupAlert)
        {
            DataTable Tasks = Session.DataManager.ExecuteDataTable("TM_GetPendingTasks", WhoAmI);
            string message = "";

            if (Tasks.Rows.Count > 0)
                message = Tasks.Rows.Count + " Bekleyen Görev";

            SetTaskMessage(message, OnTaskClicked);
        }

        void jobYeniSiparis_PopupClicked(JobManager.Job popupAlert)
        {
                  }

        void jobYeniSiparis_Alert(JobManager.Job popupAlert)
        {
                   }

        void jobOnay_Alert(JobManager.Job popupAlert)
        {
           
        }

        void jobPublish_Alert(JobManager.Job popupAlert)
        {
                    }


        public void OnTaskClicked()
        {
            ShowMdiChildForm(typeof(Tasks));
        }
    }
}